package org.devio.secondlevelmenu.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.devio.secondlevelmenu.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class SaveSelectedPositionArrayAdapter extends ArrayAdapter {
    private Activity mContext = null; // 上下文环境
    private int mResourceId; // 列表项布局资源ID
    private String[] mItems; // 列表内容数组
    private int selectedIndex = 0;
    private int mDropDownResource;

    public SaveSelectedPositionArrayAdapter(Activity context, int resId, String[] items) {
        super(context, resId, items);
        // 保存参数
        mContext = context;
        mResourceId = resId;
        mItems = items;


    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 获取LayoutInflater对象
        View itemView = LayoutInflater.from(parent.getContext()).inflate(mResourceId ,parent,false);

        TextView contentTv = (TextView) itemView.findViewById(R.id.tv_ques_type);
        String content = mItems[position];
        contentTv.setText(content);
        return itemView;
    }

    @Override
    public void setDropDownViewResource(int resource) {
        super.setDropDownViewResource(resource);
        this.mDropDownResource = resource;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(this.mDropDownResource, null);

        TextView contentTv = (TextView) itemView.findViewById(R.id.txt);
        String content = mItems[position];
        contentTv.setText(content);
        if (position == selectedIndex)
            contentTv.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
        else
            contentTv.setTextColor(mContext.getResources().getColor(R.color.color_ff3c3c3c));

        // 返回列表项视图
        return itemView;
    }

    public void refreshSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;

    }
}

